package org.dromara.pe.controller;

import java.util.Arrays;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.utils.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.pe.domain.vo.AskLeaveVo;
import org.dromara.pe.domain.bo.AskLeaveBo;
import org.dromara.pe.service.IAskLeaveService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 免测申请
 *
 * @author Lion Li
 * @date 2024-09-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pe/askLeave")
public class AskLeaveController extends BaseController {

    private final IAskLeaveService askLeaveService;

    /**
     * 查询免测申请列表
     */
    @SaCheckPermission("pe:askLeave:list")
    @GetMapping("/list")
    public TableDataInfo<AskLeaveVo> list(AskLeaveBo bo, PageQuery pageQuery) {
        return askLeaveService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出免测申请列表
     */
    @SaCheckPermission("pe:askLeave:export")
    @Log(title = "免测申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AskLeaveBo bo, HttpServletResponse response) {
        List<AskLeaveVo> list = askLeaveService.queryList(bo);
        ExcelUtil.exportExcel(list, "免测申请", AskLeaveVo.class, response);
    }

    /**
     * 获取免测申请详细信息
     *
     * @param leaveId 主键
     */
    @SaCheckPermission("pe:askLeave:query")
    @GetMapping("/{leaveId}")
    public R<AskLeaveVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long leaveId) {
        return R.ok(askLeaveService.queryById(leaveId));
    }

    /**
     * 新增免测申请
     */
    @SaCheckPermission("pe:askLeave:add")
    @Log(title = "免测申请", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AskLeaveBo bo) {
        return toAjax(askLeaveService.insertByBo(bo));
    }

    /**
     * 修改免测申请
     */
    @SaCheckPermission("pe:askLeave:edit")
    @Log(title = "免测申请", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AskLeaveBo bo) {
        return toAjax(askLeaveService.updateByBo(bo));
    }

    /**
     * 删除免测申请
     *
     * @param leaveIds 主键串
     */
    @SaCheckPermission("pe:askLeave:remove")
    @Log(title = "免测申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{leaveIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] leaveIds) {
        return toAjax(askLeaveService.deleteWithValidByIds(List.of(leaveIds), true));
    }

    /**
     * 更新审核状态     *
     * @param statusByIds
     * @param status
     */
    @Log(title = "免测申请批量审核")
    @GetMapping("/{statusByIds}/{status}")
    public R<Void> UpdateAuditStatusByIds(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] statusByIds , @PathVariable String status ) {
        List<Long> Ids = Arrays.asList(statusByIds);
        if (statusByIds.length == 0 || StringUtils.isEmpty(status)) {  //入参不能为空
            return (toAjax(false));
        }
        return toAjax(askLeaveService.updateValidByIds(Ids, status,true));
    }
}
